// pages/yuding/yuding.js
var app = getApp();
import { sevenDays } from "../../utils/getSevenDays.js";
Page({

  /**
   * 页面的初始数据
   */

  data: {
    selectedDay:"",
    isShow: false,
    checked: false,
    sevenDays:sevenDays,
    select: "",
    addressInfo: wx.getStorageSync("addressInfo"),
    calendar:{},
    curAddress: {},
    calendarConfig: {
      theme: 'default',
      highlightToday: true,
      onlyShowCurrentMonth: true,
      preventSwipe: true,
    },
    loufang: [{
        name: 'dianti',
        value: '电梯房'
      },
      {
        name: 'louti',
        value: '楼梯房'
      },
    ],
    loupan: [{
        name: 'xiaoqu',
        value: '小区'
      },
      {
        name: 'xiezilou',
        value: '写字楼'
      },
      {
        name: 'mingzhai',
        value: '民宅'
      },
    ]
  },
  showWindows: function() {
    let that = this;
    if (!wx.getStorageSync("serverFilePath")) {
      wx.showModal({
        title: '温馨提示您',
        content: '请先上传购砂申报表！',
        success(res) {
          if (res.confirm) {
            return;
          } else if (res.cancel) {
            return;
          }
        }
      });
    }
    else if (!wx.getStorageSync("addressInfo")) {
      wx.showToast({
        title: '请先选择地址！',
        icon: "none",
        duration: 2000,
        success() {
          wx.navigateTo({
            url: "/pages/yuding/dizhi/dizhi",
          });
        }
      });
    } else if (!wx.getStorageSync("deliveryTime")){
      wx.showToast({
        title: '请选择配送日期！',
        icon: "none",
        duration: 2000,
      });
    } else {
      wx.navigateTo({
        url: '/pages/yuding/goods/goods',
      });
    }
  },
  adddizhi() {
    //点击跳转到页面
    wx.navigateTo({
      url: '/pages/yuding/dizhi/dizhi',

    })
  },
  afterCalendarRender(e) {
    let that = this;
    let sevenDays = [];
    let count = [];
    /**
     * 获取七天的订单安排
     */
    wx.request({
      url: app.globalData.baseUrl + "/orders/getOrders",
      method: "GET",
      success(res) {
        count = res.data;

        console.log("count",count);

        sevenDays = that.data.sevenDays;
        
        let unChooseDays = [];
        let chooseDays = [];
        let enableCount = [];
        sevenDays.forEach((val, idx, arr) => {
          if (count[idx] < 10) {
            chooseDays.push(val);
            enableCount.push(count[idx]);
          } else {
            unChooseDays.push(val);
          }
        });
        chooseDays.forEach(val=>{
          val.class = "choose-class";
        });
        unChooseDays.forEach(val=>{
          val.class = "unchoose-class";
        });
        that.calendar.setDateStyle(chooseDays);
        that.calendar.setDateStyle(unChooseDays);
        let areaArr = []
        chooseDays.forEach((val,idx)=>{
          areaArr[idx] = val.year+ "-" + val.month + "-" + val.day;
        })
        that.calendar.enableDays(areaArr);
        chooseDays.forEach((val,idx)=>{
          val.todoText = `剩余${10 - enableCount[idx]}单`
        });
        that.calendar.setTodoLabels({
          showLabelAlways: false, 
          days: chooseDays
        });
      },
    });
  },
  afterTapDay(e) {
    let that = this;
    let selectedDay = e.detail.year+"-"+e.detail.month+"-"+e.detail.day;
    wx.setStorage({
      key: 'deliveryTime',
      data: selectedDay,
      success() {
        that.setData({
          selectedDay: selectedDay,
          select:1
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          userInfo: res.data
        })
      },
      fail(err) {
        wx.showToast({
          title: '请先登录！',
          icon: "none",
          duration: 1500,
          success: function() {
            wx.switchTab({
              url: '/pages/huiyuan/huiyuan'
            });
          }
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.calendar = this.selectComponent("#calendar");
    this.setData({
      addressInfo: wx.getStorageSync("addressInfo"),

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { 
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  uploadPhoto(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.baseUrl + "/orders/upload", //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          success(res) {
            if (res.data) {
              wx.setStorage({
                key: "serverFilePath",
                data: res.data,
                success() {
                  wx.showToast({
                    title: '协议上传成功！',
                    icon: "success",
                    duration: 2000
                  });
                }
              })
            } else {
              wx.showToast({
                title: '用户协议上传失败！',
                icon: "none",
                duration: 2000
              });
            }
          },
          fail(err) {
            wx.showToast({
              title: '用户协议上传失败！',
              icon: "none",
              duration: 2000
            });
          }
        });
      },
      fail(err) {
        wx.showToast({
          title: '微信服务异常！',
          icon: "none",
          duration: 2000
        });
      }
    });
  },
})


